package cn.wildfire.chat.kit.conversation;

import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import cn.wildfire.chat.app.extendsion.utils.TakeoverInterfaceHub;
import cn.wildfire.chat.app.main.OnRefreshGroupInfosListener;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.ConversationInfo;

public class ConversationInfoActivity extends WfcBaseActivity {

    private ConversationInfo conversationInfo;
    protected Fragment fragment = null;

    @Override
    protected int contentLayout() {
        return R.layout.fragment_container_activity;
    }

    @Override
    protected void afterViews() {
        setToolbarOrReplaceType(true, 3);
        setSearchLayoutVisibility(false);
        TakeoverInterfaceHub.setOnRefreshGroupInfosListener(onRefreshGroupInfosListener);
        conversationInfo = getIntent().getParcelableExtra("conversationInfo");
        boolean isGroup = (conversationInfo.conversation.type == Conversation.ConversationType.Group);
        if (isGroup) {
            setReplaceTitle(null, R.string.conversation_detail);
        } else {
            setReplaceTitle("", -1);
        }
        switch (conversationInfo.conversation.type) {
            case Single:
                fragment = SingleConversationInfoFragment.newInstance(conversationInfo);
                break;
            case Group:
                fragment = GroupConversationInfoFragment.newInstance(conversationInfo);
                break;
            case ChatRoom:
                // TODO
                break;
            case Channel:
                fragment = ChannelConversationInfoFragment.newInstance(conversationInfo);
                break;
            default:
                break;
        }
        if (fragment == null) {
            Toast.makeText(this, "todo", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.containerFrameLayout, fragment)
                .commit();
    }

    @Override
    protected void onDestroy() {
        TakeoverInterfaceHub.releaseRGI();
        super.onDestroy();
    }

    /**
     * 回调刷新数据
     */
    private OnRefreshGroupInfosListener onRefreshGroupInfosListener = new OnRefreshGroupInfosListener() {
        @Override
        public void refreshReq() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (fragment instanceof GroupConversationInfoFragment) {
                            ((GroupConversationInfoFragment) fragment).forceRefresh();
                            onBackPressed();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    };

}
